<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link href="../../css/bootstrap.min.css" rel="stylesheet">
		<script src="../../js/jquery-3.2.1.min.js"></script>
		<script src="../../js/bootstrap.min.js"></script>
		<script type="text/javascript" src="../../js/jquery-2.1.4.js" ></script>
		<script type="text/javascript" src="../../js/path.js" ></script>
		<style>
			
			#container {
				width: 50%;
				margin-left: 250px;
			}
			#but1{
				width: 150px;
				height: 30px;
				background-color: #bce8f1;
		
				font-family: "arial, helvetica, sans-serif";
				color: #31708f;
				border-radius: inherit;
			}
			#jieguo,#jieguo1,#jieguo2{
				color: red;
			}
		</style>
	</head>

	<body>

		<div class="panel panel-info" id="container" align="center">
			<div class="panel-heading">
				<h1 class="panel-title">添加教师</h1>
			</div>
			<div class="panel-body">
				<div style="padding: 100px 100px 10px;" align="center">
      <div class="input-group">
         <span class="input-group-addon">教师名称</span>
         <input type="text" class="form-control" placeholder="教师名称" name="teacherName" onblur="yzName();">
         <div id="jieguo1"></div>
      </div>
         	 <br>
         	 	 <div class="input-group">
         	 <span class="input-group-addon">教师工资</span>
         <input type="text" class="form-control" placeholder="教师工资" name="salary" onblur="yzSalary();">
         	<div id="jieguo2"></div>
         	 	 </div>
         	 <br>
         	 	 <div class="input-group">
         	 <span class="input-group-addon">教师职称</span>
         		<select class="allPositions">
         		
         		</select>
         	</div>
         	 <br>
         	 	<div id="jieguo"></div>
         	 	
			</div>
		</div>
		<input type="button" value="添加" id="but1"/>
		</div>
	</body>
		<script>
			var xx = getPath()+"/TeacherController";
			
			function yzName(){
			var cardNo = $('input[name="teacherName"]').val();
			if(""==cardNo ||  null == cardNo){
				$('#jieguo1').text("请重新输入...");
				return false;
			}else{
				$('#jieguo1').empty();
				return true;
			}
		}
		function yzSalary(){
			var cardNo = $('input[name="salary"]').val();
			var patten = new RegExp(/^\d{1,8}(?:\.\d{1,2})?$/);
			if(patten.test(cardNo)!=true){
			 	$('#jieguo2').text("您输入的数字有误 ，请重新输入..(只能输入两位小数)...");
			 	return false;
			 }else {
			 	$('#jieguo2').empty();
			 	return true;
			 }
			 
		}
			$('#but1').click(function(){
			  var id = $('option[name=pos]:selected')[0].value;
				 if(yzName()  && yzSalary()){
				 	 $.ajax({
					 	type:'post',
						url:xx,
						
						data:{
							method:"add",
							teacherName:$('input[name=teacherName]').val(),
							salary:$('input[name=salary]').val(),
							positionalId:id,
							},
						async:true,
						dataType:'json',
						success:function(jsonData){
							console.log(jsonData.result);
							$("#jieguo").text(jsonData.jieguo);
							if(jsonData.result=='success'){
								window.location.href="teacher-list.html";
							}
						}
					 })
				 }
				
			})
			
			$(function(){
				$.ajax({
					type:"post",
					url:getPath()+"/ChildrenController",
					data:{
						'method':'findAllPositions',
					},
					async:true,
					dataType:'json',
					success:function(jsonData){
						console.log(jsonData.result);
						if(jsonData.result=="success"){
							var data = jsonData.data;
							
							$(data).each(function(index,data){
								var childrenName = data.childrenName;
								var id = data.id;
								var op = $('<option name="pos" value="'+id+'">'+childrenName+'</option>')
								$('.allPositions').append(op);
							})
						}
					
					}
				});
			})
		</script>
	</head>
</html>
